package com.elf.bluetoothtest.activity;

import android.bluetooth.BluetoothDevice;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

import com.elf.bluetoothtest.adapter.BluetoothListAdapter;
import com.elf.bluetoothtest.bluetooth.BluetoothHelp;
import com.elf.bluetoothtest.databinding.ActivityMainBinding;
import com.elf.bluetoothtest.entity.BluetoothDeviceDetail;

public class MainActivity extends BaseActivity {

    private ActivityMainBinding binding;
    private MenuItem scanMenu;
    private BluetoothHelp bluetoothHelp;

    private BluetoothListAdapter bluetoothListAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        bluetoothHelp = BluetoothHelp.getInstance(this);
        if (bluetoothHelp.isEnable()) {
            bluetoothHelp.enable();
        }
        bluetoothHelp.setBluetoothDiscoveryListener(new BluetoothHelp.BluetoothStatusListener() {

            @Override
            public void onFoundDevice(BluetoothDevice device, short rssi) {
                BluetoothDeviceDetail deviceDetail = new BluetoothDeviceDetail();
                deviceDetail.device = device;
                deviceDetail.rssi = rssi;
                bluetoothListAdapter.add(deviceDetail);
            }

            @Override
            public void onDiscoveryFinish() {
                showToast("扫描结束");
            }

            @Override
            public void onStateOn() {

            }

            @Override
            public void onStateOff() {

            }
        });

        binding.lvBluetooth.setEmptyView(binding.tvEmpty);
        bluetoothListAdapter = new BluetoothListAdapter(this);
        binding.lvBluetooth.setAdapter(bluetoothListAdapter);
        binding.lvBluetooth.setOnItemClickListener((parent, view, position, id) -> {
            connect(bluetoothListAdapter.getItem(position).device);
        });

        scan();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        scanMenu = menu.add(1, 1, 1, "扫描");
        scanMenu.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == scanMenu.getItemId()) {
            scan();
        }
        return super.onOptionsItemSelected(item);
    }

    private void scan() {
        if (!BluetoothHelp.isSupport()) {
            showDialog("该设备不支持蓝牙");
            return;
        }
        if (!bluetoothHelp.isEnable()) {
            showDialog("请先开启蓝牙");
            return;
        }
        bluetoothListAdapter.clear();
        bluetoothHelp.discoveryBle();
        showToast("扫描中");
    }

    private void connect(BluetoothDevice device) {
        BluetoothServiceActivity.open(this, device);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        bluetoothHelp.stopDiscovery();
        bluetoothHelp.release();
    }
}